package dao;

import entity.AddrInfo;
import openConn.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDao {
    public static List<AddrInfo> select (int userId) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "select * from address_info where user_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,userId);
        ResultSet resultSet = statement.executeQuery();
        List<AddrInfo> addrInfos = new ArrayList<>();
        AddrInfo addrInfo = null;
        while (resultSet.next()){
            int addrId = resultSet.getInt("address_id");
            String province = resultSet.getString("province");
            String city = resultSet.getString("city");
            String area = resultSet.getString("area");
            String detail = resultSet.getString("detail");
            addrInfo = new AddrInfo(addrId,userId,province,city,area,detail);
            addrInfos.add(addrInfo);
        }
        resultSet.close();
        statement.close();
        connection.close();
        return addrInfos;
    }
    public static int update(AddrInfo addrInfo) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "update address_info set province = ?,city = ?,area = ?,detail = ? where address_id =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,addrInfo.getProvince());
        statement.setString(2,addrInfo.getCity());
        statement.setString(3,addrInfo.getArea());
        statement.setString(4,addrInfo.getDetail());
        statement.setInt(5,addrInfo.getAddrID());
        int count = statement.executeUpdate();
        statement.close();
        connection.close();
        return count;
    }

    public static void delete(int addrId1) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "delete from address_info where address_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,addrId1);
        int count = statement.executeUpdate();
        statement.close();
        connection.close();
    }

    public static void insert(AddrInfo addrInfo1) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "insert into address_info(user_id,province,city,area,detail) values (?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,addrInfo1.getUserID());
        statement.setString(2,addrInfo1.getProvince());
        statement.setString(3,addrInfo1.getCity());
        statement.setString(4,addrInfo1.getArea());
        statement.setString(5,addrInfo1.getDetail());
        int count = statement.executeUpdate();
        statement.close();
        connection.close();
    }
}
